<!-- 电子围栏地图页 -->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">新增围栏</span>
    <el-button type="primary" style="float:right;margin-top:13px;"
               @click="router.push('/geofence')">返回</el-button>
  </div>
  <!-- 第6步,准备地图容器 -->
  <div id="mapContainer" style="width:100%;height: 84vh;"></div>

  <!-- 新增电子围栏弹窗 -->
  <el-dialog title="围栏信息" v-model="dialogVisible" :before-close="closeDialog">
    <el-form label-width="80px">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入围栏名称" v-model="geofence.name"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="saveGeofence">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import { DrawScene, DrawControl,OperateEventType } from 'bmap-draw';
import {ElMessage} from "element-plus";
import axios from "axios";
import qs from "qs";

const dialogVisible = ref(false);

//8.定义对象用来保存围栏的名称与位置信息,新增使用
const geofence = ref({
  name:'',
  position:{}
});
var map;
onMounted(()=>{
  //表示在DOM更新完毕之后再执行,因为我们需要确保上方的地图容器div创建完毕后,再在容器中渲染地图
  nextTick(()=>{
    //第7步:创建地图示例,注意:不要引入,忽略提示!
    map = new BMapGL.Map("mapContainer");
    //第8步:设置中心点坐标，设置经纬度
    let point = new BMapGL.Point(116.4074, 39.9024);
    //第9步：设置地图显示的中心点与地图缩放级别
    map.centerAndZoom(point, 15);

    //添加鼠标绘制控件
    //1.创建绘制场景类,这是其它绘制控件使用的基类,注意:DrawScene需要导入!
    const scene = new DrawScene(map);
    //2.创建绘制控件类,并将上一回创建好的绘制场景作为构造参数传入,注意:DrawControl也需要导入!
    const drawContrl = new DrawControl(scene, {
      //3.配置绘制控件:
      enableTips: false,//不显示对应绘制项的提示文字
      anchor: BMAP_ANCHOR_TOP_RIGHT,//绘制控件定位到右上角
      drawingItems: [//设置绘制控件显示哪些绘制项  circle圆形 rectangle矩形
        'circle',
        'rectangle'
      ]
    });

    //5.监听绘制完成事件(用户点"√"时)
    scene.addEventListener(OperateEventType.COMPLETE,(event)=>{
      /* 用户点击√时,出弹窗,让用户输入围栏名称 */
      dialogVisible.value = true;
      //OperateEventType.COMPLETE 完成事件  返回触发完成[事件event]的围栏[目标对象target]
      console.log(event.target);
      //6.从触发完成事件的目标对象中获取围栏覆盖物
      let overlay = event.target.overlay;
      //7.判断绘制的是圆形还是矩形
      //instanceof用来判断左侧overlay是否为右侧BMapGL.Circle类的实例
      if(overlay instanceof BMapGL.Circle){//是圆形
        geofence.value.position.type = 'circle';
        geofence.value.position.longitude = overlay.getCenter().lng;
        geofence.value.position.latitude = overlay.getCenter().lat;
        geofence.value.position.radius = overlay.getRadius();
        console.log(geofence.value);
      }else{//是矩形
        //定义数组用来保存矩形四个顶点的坐标值
        let recPoints = [];
        //循环4次,获取4点
        for(let i = 0; i<4; i++){
          recPoints.push(overlay.points[i].latLng.lng+'-'+overlay.points[i].latLng.lat);
        }
        geofence.value.position.type = 'rectangle';
        //将4个顶点坐标值以逗号分隔拼接成字符串存入position对象的recPoints属性中
        geofence.value.position.recPoints = recPoints.join(',');
        console.log(geofence.value);
      }
    })
    //4.将绘制控件添加到地图上
    map.addControl(drawContrl);
  })
})

//定义新增围栏的方法
const saveGeofence = ()=>{
  if(!geofence.value.name || geofence.value.name.trim().length==0){
    ElMessage.error('围栏名称不能为空!');
    return;
  }
  // console.log(geofence.value);
  geofence.value.position = JSON.stringify(geofence.value.position);
  let data = qs.stringify(geofence.value);
  axios.post(BASE_URL+'/v1/geofence/save',data).then((response)=>{
    if(response.data.code == 2000){
      ElMessage.success('新增围栏成功!');
      router.push('/geofence');
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//定义处理弹窗关闭的方法
const closeDialog = ()=>{
  if(confirm('您确认要关闭吗?')){
    dialogVisible.value = false;
    map.clearOverlays();
    geofence.value={name: '', position:{}};
  }
}
</script>

<style scoped>

</style>